<template>
  <div>
    <el-form-item label="选择品种" prop="select">
      <el-button size="small" type="primary" @click="changeVariety">选择品种</el-button>
    </el-form-item>
    <el-dialog append-to-body :title="title" :visible.sync="dialogFormVisible" width="1000px" @close="close">
      <vab-query-form>
        <vab-query-form-left-panel :span="12">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.Keyword"
                clearable
                placeholder="请输入关键词"
                @clear="queryData"
                @keyup.enter.native="queryData"
              />
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            </el-form-item>
          </el-form>
        </vab-query-form-left-panel>
      </vab-query-form>
      <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows">
        <el-table-column show-overflow-tooltip type="selection" />
        <el-table-column label="品种名称" prop="breed" show-overflow-tooltip />
        <el-table-column label="品系名称" prop="strain" show-overflow-tooltip />
        <el-table-column label="批次号" prop="batchCode" show-overflow-tooltip />
        <el-table-column label="数量" prop="num" show-overflow-tooltip />
        <el-table-column label="单位" prop="unit" show-overflow-tooltip />
        <el-table-column label="性别" prop="sex" show-overflow-tooltip />
        <el-table-column label="用途" prop="use" show-overflow-tooltip />
        <el-table-column label="圈舍" prop="penHouse" show-overflow-tooltip />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { parseTime } from '@/utils/index'
  import { API_varietyQuery } from '@/api/variety'

  export default {
    name: 'VabVariety',
    data() {
      return {
        parseTime,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          // pageNo: 1,
          // pageSize: 10,
          Keyword: '',
        },
        title: '',
        dialogFormVisible: false,
        selectRows: '',
      }
    },
    created() {},
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      changeVariety() {
        this.title = '选择品种'
        this.dialogFormVisible = true
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },
      save() {
        if (this.selectRows.length > 0) {
          if (this.selectRows.length > 1) {
            return this.$baseMessage('只能勾选一行', 'warning')
          }
          this.dialogFormVisible = false
          this.$emit('selectData', this.selectRows[0])
        } else {
          this.$baseMessage('未选中任何行', 'warning')
          return false
        }
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await API_varietyQuery(this.queryForm)
        this.list = data.list
        this.total = data.count
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
